USE [master]
GO
/****** Object:  Database [AlegroPedidos]    Script Date: 03/08/2015 12:13:10 ******/
CREATE DATABASE [AlegroPedidos] ON  PRIMARY 
( NAME = N'AlegroPedidos', FILENAME = N'c:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\AlegroPedidos.mdf' , SIZE = 2304KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
 LOG ON 
( NAME = N'AlegroPedidos_log', FILENAME = N'c:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\AlegroPedidos_log.LDF' , SIZE = 832KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
GO
ALTER DATABASE [AlegroPedidos] SET COMPATIBILITY_LEVEL = 100
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [AlegroPedidos].[dbo].[sp_fulltext_database] @action = 'enable'
end
GO
ALTER DATABASE [AlegroPedidos] SET ANSI_NULL_DEFAULT OFF
GO
ALTER DATABASE [AlegroPedidos] SET ANSI_NULLS OFF
GO
ALTER DATABASE [AlegroPedidos] SET ANSI_PADDING OFF
GO
ALTER DATABASE [AlegroPedidos] SET ANSI_WARNINGS OFF
GO
ALTER DATABASE [AlegroPedidos] SET ARITHABORT OFF
GO
ALTER DATABASE [AlegroPedidos] SET AUTO_CLOSE ON
GO
ALTER DATABASE [AlegroPedidos] SET AUTO_CREATE_STATISTICS ON
GO
ALTER DATABASE [AlegroPedidos] SET AUTO_SHRINK OFF
GO
ALTER DATABASE [AlegroPedidos] SET AUTO_UPDATE_STATISTICS ON
GO
ALTER DATABASE [AlegroPedidos] SET CURSOR_CLOSE_ON_COMMIT OFF
GO
ALTER DATABASE [AlegroPedidos] SET CURSOR_DEFAULT  GLOBAL
GO
ALTER DATABASE [AlegroPedidos] SET CONCAT_NULL_YIELDS_NULL OFF
GO
ALTER DATABASE [AlegroPedidos] SET NUMERIC_ROUNDABORT OFF
GO
ALTER DATABASE [AlegroPedidos] SET QUOTED_IDENTIFIER OFF
GO
ALTER DATABASE [AlegroPedidos] SET RECURSIVE_TRIGGERS OFF
GO
ALTER DATABASE [AlegroPedidos] SET  ENABLE_BROKER
GO
ALTER DATABASE [AlegroPedidos] SET AUTO_UPDATE_STATISTICS_ASYNC OFF
GO
ALTER DATABASE [AlegroPedidos] SET DATE_CORRELATION_OPTIMIZATION OFF
GO
ALTER DATABASE [AlegroPedidos] SET TRUSTWORTHY OFF
GO
ALTER DATABASE [AlegroPedidos] SET ALLOW_SNAPSHOT_ISOLATION OFF
GO
ALTER DATABASE [AlegroPedidos] SET PARAMETERIZATION SIMPLE
GO
ALTER DATABASE [AlegroPedidos] SET READ_COMMITTED_SNAPSHOT OFF
GO
ALTER DATABASE [AlegroPedidos] SET HONOR_BROKER_PRIORITY OFF
GO
ALTER DATABASE [AlegroPedidos] SET  READ_WRITE
GO
ALTER DATABASE [AlegroPedidos] SET RECOVERY SIMPLE
GO
ALTER DATABASE [AlegroPedidos] SET  MULTI_USER
GO
ALTER DATABASE [AlegroPedidos] SET PAGE_VERIFY CHECKSUM
GO
ALTER DATABASE [AlegroPedidos] SET DB_CHAINING OFF
GO
USE [AlegroPedidos]
GO
/****** Object:  Table [dbo].[tbl_produto]    Script Date: 03/08/2015 12:13:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[tbl_produto](
	[pro_codigo] [int] NOT NULL,
	[ped_codigo] [int] NOT NULL,
	[pro_quantidade] [int] NULL,
	[pro_descricao] [varchar](255) NULL,
	[pro_ipi] [varchar](50) NULL,
	[pro_valor_unitario] [varchar](50) NULL,
	[pro_valor_unitario_com_ipi] [varchar](50) NULL,
	[pro_valor_total_produto] [varchar](50) NULL,
	[pro_valor_total_produto_com_ipi] [varchar](50) NULL,
	[pro_comissao_vendedor] [varchar](10) NULL,
	[pro_comissao_cliente] [varchar](10) NULL,
	[pro_status] [int] NULL,
	[pro_excluido] [int] NULL,
PRIMARY KEY CLUSTERED 
(
	[pro_codigo] ASC,
	[ped_codigo] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[tbl_justificativa]    Script Date: 03/08/2015 12:13:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[tbl_justificativa](
	[jus_texto_justificativa] [text] NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
/****** Object:  Table [dbo].[tbl_estado]    Script Date: 03/08/2015 12:13:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[tbl_estado](
	[est_codigo] [int] NOT NULL,
	[est_nome] [varchar](150) NOT NULL,
	[est_uf] [varchar](2) NOT NULL,
PRIMARY KEY CLUSTERED 
(
	[est_codigo] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[tbl_tipo_usuario]    Script Date: 03/08/2015 12:13:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[tbl_tipo_usuario](
	[tip_codigo_tipo_usuario] [int] IDENTITY(1,1) NOT NULL,
	[tpusu_nome] [varchar](35) NULL,
PRIMARY KEY CLUSTERED 
(
	[tip_codigo_tipo_usuario] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[tbl_tipo_cliente]    Script Date: 03/08/2015 12:13:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[tbl_tipo_cliente](
	[tpcli_codigo] [int] IDENTITY(1,1) NOT NULL,
	[tpcli_nome] [varchar](35) NULL,
PRIMARY KEY CLUSTERED 
(
	[tpcli_codigo] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[tbl_sequence]    Script Date: 03/08/2015 12:13:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[tbl_sequence](
	[seq_pedido] [int] NOT NULL,
	[seq_cliente] [int] NOT NULL
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[tbl_prazo]    Script Date: 03/08/2015 12:13:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[tbl_prazo](
	[pra_codigo] [int] IDENTITY(1,1) NOT NULL,
	[pra_dias] [varchar](255) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  UserDefinedFunction [dbo].[Varchar2float]    Script Date: 03/08/2015 12:13:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[Varchar2float]
(-- Input Parameter
@inputString VARCHAR(50))
RETURNS FLOAT
AS
BEGIN
--Prepare the string for casting/conversion
SET @inputString = Replace(@inputString,'.','') 
SET @inputString = Replace(@inputString,',','.')

--Perform the conversion and return the result
RETURN Cast(@inputString AS FLOAT)
END
GO
/****** Object:  Table [dbo].[tbl_pedido]    Script Date: 03/08/2015 12:13:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[tbl_pedido](
	[ped_codigo] [int] IDENTITY(1,1) NOT NULL,
	[cli_codigo] [int] NOT NULL,
	[usu_codigo] [int] NOT NULL,
	[usu_vendedor_singem] [varchar](2) NULL,
	[ped_emissao] [datetime] NULL,
	[ped_emisso] [varchar](50) NULL,
	[ped_prioridade] [varchar](10) NULL,
	[ped_tabela] [varchar](10) NULL,
	[ped_desconto] [varchar](50) NULL,
	[ped_prazo] [varchar](50) NULL,
	[ped_valor_total] [varchar](50) NULL,
	[ped_valor_total_com_ipi] [varchar](50) NULL,
	[ped_valor_total_com_desconto] [varchar](50) NULL,
	[ped_status_comercial] [int] NULL,
	[ped_status_financeiro] [int] NULL,
	[ped_motivo_reprovacao_comercial] [varchar](50) NULL,
	[ped_motivo_reprovacao_financeiro] [varchar](50) NULL,
	[ped_observacoes] [text] NULL,
	[ped_observacao_financeiro] [text] NULL,
	[ped_observacao_comercial] [text] NULL,
	[ped_motivo_cancelamento] [varchar](255) NULL,
	[ped_responsavel] [varchar](50) NULL,
	[ped_documento] [varchar](50) NULL,
	[ped_comissao_vendedor] [varchar](50) NULL,
	[ped_comissao_cliente] [varchar](50) NULL,
	[ped_tipo_tp_recebimento] [varchar](2) NULL,
	[ped_peso_bruto] [varchar](50) NULL,
	[ped_peso_liquido] [varchar](50) NULL,
	[ped_cancelado] [datetime] NULL,
	[ped_entrega] [datetime] NULL,
	[ped_status] [int] NULL,
	[ped_excluido] [int] NULL,
 CONSTRAINT [PK__tbl_pedi__E25C09A85629CD9C] PRIMARY KEY CLUSTERED 
(
	[ped_codigo] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[tbl_usuario]    Script Date: 03/08/2015 12:13:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[tbl_usuario](
	[usu_codigo] [int] IDENTITY(1,1) NOT NULL,
	[tip_codigo_tipo_usuario] [int] NOT NULL,
	[usu_nome] [varchar](255) NULL,
	[usu_email] [varchar](50) NULL,
	[usu_senha] [varchar](50) NULL,
	[usu_status] [int] NULL,
	[usu_excluido] [int] NULL,
	[usu_codigo_singem] [varchar](2) NULL,
	[usu_grupo] [varchar](2) NULL,
 CONSTRAINT [PK__tbl_usua__F232FBD11DE57479] PRIMARY KEY CLUSTERED 
(
	[usu_codigo] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[tbl_cidade]    Script Date: 03/08/2015 12:13:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[tbl_cidade](
	[cid_codigo] [int] NOT NULL,
	[est_codigo] [int] NOT NULL,
	[cid_nome] [varchar](150) NOT NULL,
PRIMARY KEY CLUSTERED 
(
	[cid_codigo] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[tbl_cliente]    Script Date: 03/08/2015 12:13:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[tbl_cliente](
	[cli_codigo] [int] IDENTITY(1,1) NOT NULL,
	[usu_codigo] [int] NOT NULL,
	[tpcli_tipo] [int] NULL,
	[cli_nome] [varchar](100) NULL,
	[cli_endereco] [varchar](255) NULL,
	[cli_complemento] [varchar](255) NULL,
	[cli_bairro] [varchar](100) NULL,
	[cid_codigo] [int] NULL,
	[est_codigo] [int] NULL,
	[cli_cep] [varchar](10) NULL,
	[cli_fantasia] [varchar](255) NULL,
	[cli_inscricao] [varchar](20) NULL,
	[cli_cgc] [varchar](25) NULL,
	[cli_contato] [varchar](50) NULL,
	[cli_fone] [varchar](15) NULL,
	[cli_email] [varchar](50) NULL,
	[cli_observacao] [text] NULL,
	[cli_pre_cadastro] [int] NULL,
	[cli_data_cadastro] [date] NOT NULL,
	[cli_status] [int] NULL,
	[cli_excluido] [int] NULL,
PRIMARY KEY CLUSTERED 
(
	[cli_codigo] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object:  Default [DF__tbl_seque__seq_p__1273C1CD]    Script Date: 03/08/2015 12:13:11 ******/
ALTER TABLE [dbo].[tbl_sequence] ADD  DEFAULT ((0)) FOR [seq_pedido]
GO
/****** Object:  Default [DF__tbl_seque__seq_c__1367E606]    Script Date: 03/08/2015 12:13:11 ******/
ALTER TABLE [dbo].[tbl_sequence] ADD  DEFAULT ((0)) FOR [seq_cliente]
GO
/****** Object:  ForeignKey [FK__tbl_produ__ped_c__5CD6CB2B]    Script Date: 03/08/2015 12:13:11 ******/
ALTER TABLE [dbo].[tbl_produto]  WITH CHECK ADD  CONSTRAINT [FK__tbl_produ__ped_c__5CD6CB2B] FOREIGN KEY([ped_codigo])
REFERENCES [dbo].[tbl_pedido] ([ped_codigo])
GO
ALTER TABLE [dbo].[tbl_produto] CHECK CONSTRAINT [FK__tbl_produ__ped_c__5CD6CB2B]
GO
/****** Object:  ForeignKey [FK__tbl_pedid__usu_c__5812160E]    Script Date: 03/08/2015 12:13:11 ******/
ALTER TABLE [dbo].[tbl_pedido]  WITH CHECK ADD  CONSTRAINT [FK__tbl_pedid__usu_c__5812160E] FOREIGN KEY([usu_codigo])
REFERENCES [dbo].[tbl_usuario] ([usu_codigo])
GO
ALTER TABLE [dbo].[tbl_pedido] CHECK CONSTRAINT [FK__tbl_pedid__usu_c__5812160E]
GO
/****** Object:  ForeignKey [FK__tbl_usuar__tip_c__267ABA7A]    Script Date: 03/08/2015 12:13:11 ******/
ALTER TABLE [dbo].[tbl_usuario]  WITH CHECK ADD  CONSTRAINT [FK__tbl_usuar__tip_c__267ABA7A] FOREIGN KEY([tip_codigo_tipo_usuario])
REFERENCES [dbo].[tbl_tipo_usuario] ([tip_codigo_tipo_usuario])
GO
ALTER TABLE [dbo].[tbl_usuario] CHECK CONSTRAINT [FK__tbl_usuar__tip_c__267ABA7A]
GO
/****** Object:  ForeignKey [FK__tbl_cidad__est_c__1FCDBCEB]    Script Date: 03/08/2015 12:13:11 ******/
ALTER TABLE [dbo].[tbl_cidade]  WITH CHECK ADD FOREIGN KEY([est_codigo])
REFERENCES [dbo].[tbl_estado] ([est_codigo])
GO
/****** Object:  ForeignKey [FK__tbl_clien__cid_c__20C1E124]    Script Date: 03/08/2015 12:13:11 ******/
ALTER TABLE [dbo].[tbl_cliente]  WITH CHECK ADD FOREIGN KEY([cid_codigo])
REFERENCES [dbo].[tbl_cidade] ([cid_codigo])
GO
/****** Object:  ForeignKey [FK__tbl_clien__est_c__21B6055D]    Script Date: 03/08/2015 12:13:11 ******/
ALTER TABLE [dbo].[tbl_cliente]  WITH CHECK ADD FOREIGN KEY([est_codigo])
REFERENCES [dbo].[tbl_estado] ([est_codigo])
GO
/****** Object:  ForeignKey [FK__tbl_clien__tpcli__22AA2996]    Script Date: 03/08/2015 12:13:11 ******/
ALTER TABLE [dbo].[tbl_cliente]  WITH CHECK ADD FOREIGN KEY([tpcli_tipo])
REFERENCES [dbo].[tbl_tipo_cliente] ([tpcli_codigo])
GO
/****** Object:  ForeignKey [FK__tbl_clien__usu_c__239E4DCF]    Script Date: 03/08/2015 12:13:11 ******/
ALTER TABLE [dbo].[tbl_cliente]  WITH CHECK ADD  CONSTRAINT [FK__tbl_clien__usu_c__239E4DCF] FOREIGN KEY([usu_codigo])
REFERENCES [dbo].[tbl_usuario] ([usu_codigo])
GO
ALTER TABLE [dbo].[tbl_cliente] CHECK CONSTRAINT [FK__tbl_clien__usu_c__239E4DCF]
GO
